/**
 * @param {number[]} list
 * @returns {number}
 */
const sum = (list) => list.reduce((acc, val) => acc + val, 0);
const n = 2;
const d = [3, 8];
const LR = [3, 6];
/**
 * @param {number[]} d
 * @param {[L :number, R: number]} LR
 * @returns {number | undefined}
 */
function p10(d, [L, R]) {
  if (sum(d) < L * d.length || sum(d) > R * d.length) {
    console.log(-1);
  } else {
    let sa = 0;
    let sb = 0;
    for (let i = 0; i < d.length; i++) {
      if (d[i] < L) sa += L - d[i];
      else if (d[i] > R) sb += d[i] - R;
    }
    return Math.abs(sa - sb) + Math.min(sa, sb);
  }
}
